// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UIChooser.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.UI.Forms
{
    #region Using Directives

    using System.Windows.Forms;

    using Org.OpenTrader.Framework.Events;

    #endregion

    /// <summary>
    /// The ui chooser.
    /// </summary>
    public partial class UIChooser : UIControl
    {
        #region Constants and Fields

        /// <summary>
        /// The list.
        /// </summary>
        private readonly Item[] list;

        /// <summary>
        /// The selected.
        /// </summary>
        private Item selected;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UIChooser"/> class.
        /// </summary>
        public UIChooser()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIChooser"/> class.
        /// </summary>
        /// <param name="list">
        /// The list.
        /// </param>
        /// <param name="rowsToDisplay">
        /// The rowsToDisplay.
        /// </param>
        /// <param name="headerText">
        /// The headerText.
        /// </param>
        public UIChooser(Item[] list, int rowsToDisplay, string headerText)
        {
            this.list = list;
            this.InitializeComponent();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets SelectedItem.
        /// </summary>
        public Item SelectedItem
        {
            get
            {
                return this.selected;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The virtualGrid_ ui grid query cell info.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnVirtualGridUIGridQueryCellInfo(object sender, VirtualGridEventArgs.QueryCellInfo e)
        {
            switch (e.ColumnIndex)
            {
                case 0:
                    e.Value = this.list[e.RowIndex].Text;
                    break;
            }
        }

        /// <summary>
        /// The virtualGrid_ ui grid query row count.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnVirtualGridQueryRowCount(object sender, VirtualGridEventArgs.QueryRowCount e)
        {
            e.RowCount = this.list.Length;
        }

        /// <summary>
        /// The virtualGrid_ cell double click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnVirtualGridCellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= this.list.Length)
            {
                return;
            }

            this.selected = this.list[e.RowIndex];
            this.ContainingForm.Destroy();
        }

        #endregion

        /// <summary>
        /// The item.
        /// </summary>
        public class Item
        {
            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="Item"/> class.
            /// </summary>
            /// <param name="text">
            /// The text.
            /// </param>
            /// <param name="tag">
            /// The tag.
            /// </param>
            public Item(string text, object tag)
            {
                this.Text = text;
                this.Tag = tag;
            }

            #endregion

            #region Properties

            /// <summary>
            /// The tag.
            /// </summary>
            public object Tag { get; set; }

            /// <summary>
            /// The text.
            /// </summary>
            public string Text { get; set; }

            #endregion
        }
    }
}